package utils

import (
	"fmt"
	"strconv"
	"strings"
)

// Capitalize 字符首字母大写
func Capitalize(str string) string {
	var upperStr string
	vv := []rune(str) // 后文有介绍
	for i := 0; i < len(vv); i++ {
		if i == 0 {
			if vv[i] >= 97 && vv[i] <= 122 { // 后文有介绍
				vv[i] -= 32 // string的码表相差32位
				upperStr += string(vv[i])
			} else {
				fmt.Println("Not begins with lowercase letter,")
				return str
			}
		} else {
			upperStr += string(vv[i])
		}
	}
	return upperStr
}

// InnerLinkReplaceEach 内链域名特殊字符替换
func InnerLinkReplaceEach(str string) string {
	str = strings.ReplaceAll(str, "http://", "")
	str = strings.ReplaceAll(str, "https://", "")
	str = strings.ReplaceAll(str, "www.", "")
	str = strings.ReplaceAll(str, ".", "/")
	return str
}

// IdsStrToInt64 "1,2,3"转[1,2,3]
func IdsStrToInt64(s string) []int64 {
	lines := strings.Split(s, ",")
	r := make([]int64, len(lines))
	for i, item := range lines {
		id, _ := strconv.ParseInt(item, 10, 64)
		r[i] = id
	}
	return r
}

// IdsStrToInt32 "1,2,3"转[1,2,3]
func IdsStrToInt32(s string) []int32 {
	lines := strings.Split(s, ",")
	r := make([]int32, len(lines))
	for i, item := range lines {
		id, _ := strconv.ParseInt(item, 10, 32)
		r[i] = int32(id)
	}
	return r
}

// IdsContains ids是否包含id
func IdsContains(ids []int64, id int64) bool {
	for _, item := range ids {
		if item == id {
			return true
		}
	}
	return false
}

// IsHttp 传入的字符串是否为http或https格式
func IsHttp(url string) bool {
	if len(url) < 7 {
		return false
	}
	if url[:7] == "http://" {
		return true
	}
	if len(url) < 8 {
		return false
	}
	return url[:8] == "https://"
}
